package com.shop.admin.controller;

import com.shop.admin.aop.Log;
import com.shop.admin.service.ICategeryService;
import com.shop.admin.validate.category.CategeryCreateValidate;
import com.shop.admin.validate.category.CategerySearchValidate;
import com.shop.admin.validate.category.CategeryUpdateValidate;
import com.shop.admin.validate.commons.IdValidate;
import com.shop.admin.validate.commons.PageValidate;
import com.shop.admin.vo.category.CategoryDetailVo;
import com.shop.admin.vo.category.CategoryListedVo;
import com.shop.common.core.AjaxResult;
import com.shop.common.core.PageResult;
import com.shop.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("${app.api-prefix}/categery")
@Api(tags = "课程分类管理")
public class CategeryController {

    @Resource
    ICategeryService iCategeryService;

    @GetMapping("/list")
    @ApiOperation(value="课程分类列表")
    public AjaxResult<PageResult<CategoryListedVo>> list(@Validated PageValidate pageValidate,
                                                         @Validated CategerySearchValidate searchValidate) {
        PageResult<CategoryListedVo> list = iCategeryService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @GetMapping("/detail")
    @ApiOperation(value="课程分类详情")
    public AjaxResult<CategoryDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        CategoryDetailVo detail = iCategeryService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "课程分类新增")
    @PostMapping("/add")
    @ApiOperation(value="课程分类新增")
    public AjaxResult<Object> add(@Validated @RequestBody CategeryCreateValidate createValidate) {
        iCategeryService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "课程分类编辑")
    @PostMapping("/edit")
    @ApiOperation(value="课程分类编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody CategeryUpdateValidate updateValidate) {
        iCategeryService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "课程分类删除")
    @PostMapping("/del")
    @ApiOperation(value="课程分类删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iCategeryService.del(idValidate.getId());
        return AjaxResult.success();
    }

}
